package org.moyrax.compressor;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

/**
 * This class is the default error reporting strategy. It will handle
 * compression and bundling errors.
 *
 * @author Matias Mirabelli &lt;lumen.night@gmail.com&gt;
 * @since 0.1.0
 */
public class CompressorErrorReporter implements ErrorReporter {
  /**
   * Report an error.
   *
   * @param message     A String describing the error.
   * @param sourceName  A String describing the source where the error occured;
   *                    typically a filename or URL.
   * @param line        The line number associated with the error.
   * @param lineSource  The text of the line (may be null).
   * @param lineOffset  The offset into lineSource where problem was detected.
   */
  public void error(final String message, final String sourceName,
      final int line, final String lineSource, final int lineOffset) {

    final EvaluatorException evaluatorEx =  this.runtimeError(message,
        sourceName, line, lineSource, lineOffset);

    throw new CompressorException(evaluatorEx);
  }

  /**
   * Creates an EvaluatorException that may be thrown.
   *
   * @param message     A String describing the error.
   * @param sourceName  A String describing the source where the error occured;
   *                    typically a filename or URL.
   * @param line        The line number associated with the error.
   * @param lineSource  The text of the line (may be null).
   * @param lineOffset  The offset into lineSource where problem was detected.
   */
  public EvaluatorException runtimeError(final String message,
      final String sourceName, final int line, final String lineSource,
      final int lineOffset) {

    return new EvaluatorException(message, sourceName, line, lineSource,
        lineOffset);
  }

  /**
   * Report a warning.
   *
   * @param message     A String describing the warning.
   * @param sourceName  A String describing the source where the warning
   *                    occured; typically a filename or URL.
   * @param line        The line number associated with the error.
   * @param lineSource  The text of the line (may be null).
   * @param lineOffset  The offset into lineSource where problem was detected.
   */
  public void warning(final String message, final String sourceName,
      final int line, final String lineSource, final int lineOffset) {

    final EvaluatorException evaluatorEx =  this.runtimeError(message,
        sourceName, line, lineSource, lineOffset);

    final CompressorException ex = new CompressorException(evaluatorEx);

    System.out.println(ex.toString());
  }
}
